package org.example.managementsystem.service;

import org.example.managementsystem.model.LeaveApplication;
import org.example.managementsystem.model.LeaveApproval;
import org.example.managementsystem.model.Schedule;
import org.example.managementsystem.repository.LeaveApplicationDao;
import org.example.managementsystem.repository.LeaveApprovalDao;
import org.example.managementsystem.repository.ScheduleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LeaveService {

    @Autowired
    private LeaveApplicationDao leaveApplicationDao;

    @Autowired
    private LeaveApprovalDao leaveApprovalDao;

    @Autowired
    private ScheduleDao scheduleDao;

    /**
     * 提交请假申请
     * 
     * @param application 请假申请信息
     * @return 保存后的请假申请
     */
    public LeaveApplication submitLeaveApplication(LeaveApplication application) {
        // 验证排班记录存在
        Schedule schedule = scheduleDao.findById(application.getScheduleId());
        if (schedule == null) {
            throw new IllegalArgumentException("排班记录不存在");
        }
        
        // 验证替班人员工号有效
        if (application.getSubstituteJobNumber() == null || application.getSubstituteJobNumber().isEmpty()) {
            throw new IllegalArgumentException("必须指定替班人员");
        }
        
        // 设置初始状态为未审批
//        application.setIsApproved(false);
        
        // 保存请假申请
        return leaveApplicationDao.save(application);
    }

    /**
     * 审批请假申请
     * 
     * @param approval 审批信息
     * @return 处理结果
     */
    public Map<String, Object> approveLeaveApplication(LeaveApproval approval) {
        Map<String, Object> result = new HashMap<>();
        
        // 查找请假申请
        LeaveApplication application = leaveApplicationDao.findById(approval.getApplicationId());
        if (application == null) {
            result.put("success", false);
            result.put("message", "请假申请不存在");
            return result;
        }
        
        // 更新请假申请的审批状态
        application.setIsApproved(approval.getIsApproved());
        leaveApplicationDao.save(application);
        
        // 保存审批记录
        leaveApprovalDao.save(approval);
        
        // 如果审批通过，更新排班表中的替班信息
        if (approval.getIsApproved()) {
            Schedule schedule = scheduleDao.findById(application.getScheduleId());
            if (schedule != null) {
                // 将原排班设为非活动
                schedule.setIsActive(false);
                scheduleDao.save(schedule);
                
                // 创建替班人员的排班记录
                Schedule substituteSchedule = new Schedule();
                substituteSchedule.setJobNumber(application.getSubstituteJobNumber());
                substituteSchedule.setScheduleTime(schedule.getScheduleTime());
                substituteSchedule.setDepartment(schedule.getDepartment());
                substituteSchedule.setIsActive(true);
                scheduleDao.save(substituteSchedule);
            }
        }
        
        result.put("success", true);
        result.put("message", approval.getIsApproved() ? "请假申请已批准" : "请假申请已拒绝");
        return result;
    }

    /**
     * 获取所有请假申请
     */
    public List<LeaveApplication> getAllLeaveApplications() {
        return leaveApplicationDao.findAll();
    }

    /**
     * 获取指定排班的请假申请
     */
    public List<LeaveApplication> getLeaveApplicationsByScheduleId(Long scheduleId) {
        return leaveApplicationDao.findByScheduleId(scheduleId);
    }

    /**
     * 获取请假审批详情
     */
    public LeaveApproval getLeaveApprovalById(Long applicationId) {
        return leaveApprovalDao.findById(applicationId);
    }

    /**
     * 根据员工工号获取请假申请
     * 
     * @param jobNumber 员工工号
     * @return 请假申请列表
     */
    public List<LeaveApplication> getLeaveApplicationsByEmployeeJobNumber(String jobNumber) {
        // 1. 查询该员工的所有排班记录
        List<Schedule> schedules = scheduleDao.findByJobNumber(jobNumber);
        
        // 如果没有排班记录，返回空列表
        if (schedules == null || schedules.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 2. 根据排班ID查询对应的请假申请
        List<LeaveApplication> result = new ArrayList<>();
        for (Schedule schedule : schedules) {
            List<LeaveApplication> applications = leaveApplicationDao.findByScheduleId(schedule.getId());
            if (applications != null && !applications.isEmpty()) {
                result.addAll(applications);
            }
        }
        
        return result;
    }
}